package com.yanqu.road.server.cmd.monopolymarket;

import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.monopolymarket.MonopolyMarket;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketOfficeMgr;
import com.yanqu.road.server.manager.monopolymarket.MonopolyMarketResGrabMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/12 17:08
 */
@Cmd(code = Protocol.C_CROSS_MONOPOLY_MARKET_EXIT, desc = "玩家离线 和离开榷署座位")
public class CrossMonopolyMarketEixtCmd extends CrossCommand {

    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketExitReqMsg req = MonopolyMarketProto.MonopolyMarketExitReqMsg.parseFrom(packet.getBytes());
        MonopolyMarket monopolyMarket = MonopolyMarketMgr.getMonopolyMarket(serverId,true);
        if(monopolyMarket == null){
            return;
        }
        synchronized (monopolyMarket){
            if(req.getIsOffOnline() || req.getIsExitMonopolyMarket()){
                MonopolyMarketMgr.exitAllSeat(monopolyMarket,userId);
                return;
            }
            if(req.getExitOfficeSeatIndex() > 0 ){
                MonopolyMarketOfficeMgr.exitOfficeSeat(monopolyMarket,req.getExitOfficeSeatIndex(),userId);
            }
            if(req.getExitResGrabKingSeatIndex() > 0){
                MonopolyMarketResGrabMgr.exitPoint(monopolyMarket,eMonopolyMarketResType.RES_KING.getValue(),req.getExitOfficeSeatIndex(),userId);
            }
            if(req.getExitResGrabQueenSeatIndex() > 0){
                MonopolyMarketResGrabMgr.exitPoint(monopolyMarket,eMonopolyMarketResType.RES_QUEEN.getValue(),req.getExitOfficeSeatIndex(),userId);
            }
        }
    }

    @Override
    public void sendData() {

    }
}
